package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appSaleSiteDao")
public class AppSaleSiteDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppSaleSite queryWithKeys(String saleSiteId) throws SQLException {
		AppSaleSite dc = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM app_sale_site WHERE sale_site_id=?");
			ps.setString(1, saleSiteId);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new AppSaleSite();
				dc.setSaleSiteId(rs.getString("sale_site_id"));
				dc.setSaleSiteName(rs.getString("sale_site_name"));
				dc.setAppId(rs.getString("app_id"));
				dc.setSaleSiteAddress(rs.getString("sale_site_address"));
				dc.setSaleSiteLongitude(rs.getString("sale_site_longitude"));
				dc.setSaleSiteLatitude(rs.getString("sale_site_latitude"));
				dc.setSaleSiteType(rs.getString("sale_site_type"));
				dc.setSaleSiteDesc(rs.getString("sale_site_desc"));
				dc.setSaleSiteTel(rs.getString("sale_site_tel"));
				if ("BS".equals(rs.getString("sale_site_type"))) {
					dc.setSaleSiteTypeCN("商户");
				} else if ("SS".equals(rs.getString("sale_site_type"))) {
					dc.setSaleSiteTypeCN("网点");
				} else {
					dc.setSaleSiteTypeCN("");
				}

			}
		} catch (SQLException e) {
			throw new SQLException("queryAppSaleSiteWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return dc;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppSaleSite appSaleSite) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		int flag;
		try {
			String sql = "insert into app_sale_site values(?,?,?,?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appSaleSite.getSaleSiteId());
			ps.setString(2, appSaleSite.getSaleSiteName());
			ps.setString(3, appSaleSite.getAppId());
			ps.setString(4, appSaleSite.getSaleSiteAddress());
			ps.setString(5, appSaleSite.getSaleSiteLongitude());
			ps.setString(6, appSaleSite.getSaleSiteLatitude());
			ps.setString(7, appSaleSite.getSaleSiteType());
			ps.setString(8, appSaleSite.getSaleSiteDesc());
			ps.setString(9, appSaleSite.getSaleSiteTel());
			flag = ps.executeUpdate();

		} catch (SQLException e) {
			throw new SQLException("insertAppSaleSite is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppSaleSite appSaleSite) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update app_sale_site set sale_site_name=? , "
					+ "sale_site_address=? ,sale_site_longitude=? , "
					+ "sale_site_latitude=? ,sale_site_type=? , sale_site_desc=? ,"
					+ "sale_site_tel=? where sale_site_id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appSaleSite.getSaleSiteName());
			ps.setString(2, appSaleSite.getSaleSiteAddress());
			ps.setString(3, appSaleSite.getSaleSiteLongitude());
			ps.setString(4, appSaleSite.getSaleSiteLatitude());
			ps.setString(5, appSaleSite.getSaleSiteType());
			ps.setString(6, appSaleSite.getSaleSiteDesc());
			ps.setString(7, appSaleSite.getSaleSiteTel());
			ps.setString(8, appSaleSite.getSaleSiteId());
			flag = ps.executeUpdate();

		} catch (SQLException e) {
			throw new SQLException("updateAppSaleSiteWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(AppSaleSite appSaleSite) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from app_sale_site where sale_site_id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appSaleSite.getSaleSiteId());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("deleteAppSaleSiteWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
